package service

import (
	"context"

	"gitee.com/thewalkers/refactoring-code/model"
	"go.uber.org/zap"
)

// MailMethodService acts as a struct for injecting an implementation of EmployeeRepository
// for use in service methods
type MailMethodService struct {
	MailMethodRepository    model.MailMethodRepository
	PaymentMethodRepository model.PaymentMethodRepository
}

// MMConfig will hold repository that will eventually be injected into this
// service layer
type MMConfig struct {
	MailMethodRepository    model.MailMethodRepository
	PaymentMethodRepository model.PaymentMethodRepository
}

// NewMailMethodService is a factory function for
// initializing a EmployeeService with its repository layer dependencies
func NewMailMethodService(m *MMConfig) model.MailMethodService {
	return &MailMethodService{
		MailMethodRepository:    m.MailMethodRepository,
		PaymentMethodRepository: m.PaymentMethodRepository,
	}
}

func (m *MailMethodService) Create(ctx context.Context, id int64, mail *model.MailMethod) error {
	paymentMethod, err := m.PaymentMethodRepository.FindByEmlpoyeeID(ctx, id)
	if err != nil {
		zap.L().Error("service can not find payment method", zap.Error(err))
		return err
	}

	mail.PaymentMethodID = paymentMethod.ID
	err = m.MailMethodRepository.Create(ctx, mail)

	return err
}

func (m *MailMethodService) Update(ctx context.Context, id int64, mail *model.MailMethod) error {
	paymentMethod, err := m.PaymentMethodRepository.FindByEmlpoyeeID(ctx, id)
	if err != nil {
		zap.L().Error("service can not find payment method", zap.Error(err))
		return err
	}

	mail.PaymentMethodID = paymentMethod.ID
	err = m.MailMethodRepository.Update(ctx, mail)

	return err
}
